//cache缓存
import Cache from '@/utils/cache.js';
//语言
import langmatch from '@/utils/langmatch.js';
//赛事
import eventslistlib from '@/lib/eventslistlib.js'
//赛事用户
import eventuserslib from '@/lib/eventuserslib.js'
//基础
import Base from '@/common/base.js'

//错误提示
const homeUrl = async function() {
	Base.publicpopup(langmatch.lang.infoError, langmatch.lang.authorisationerrordesc);
	return false;
}

//授权得到用户
const getwxUserLibMethod = async function(data = {}) {
	var that=this;
	var backmsg = await eventuserslib.getUserLib(data);
	if (backmsg.data.success) {
		if (backmsg.data.data.code == 10000) {
			var data={};
			data['openid']=Cache.get('user_openid');
		    that.inserUserLibMethod(data,'');
		} else {
			 Cache.set('user_id',backmsg.data.data.id);
			 Base.reload();
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}


//首页
const getindexdatLibMethod = async function(data = {}) {
	var backmsg = await eventslistlib.getindexdatLib(data);
	if (backmsg.data.success) {
		//成功
		return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//得到赛季列表
const getSeasonListLibMethod = async function(data = {}) {
	var backmsg = await eventslistlib.getSeasonListLib(data);
	if (backmsg.data.success) {
		//成功
		return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//得到赛事列表
const getMakeListLibMethod = async function(data = {}) {
	var backmsg = await eventslistlib.getMakeListLib(data);
	if (backmsg.data.success) {
		//成功
		return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//得到奖品
const getPrizeListLibMethod = async function(data = {}) {
	var backmsg = await eventslistlib.getPrizeListLib(data);
	if (backmsg.data.success) {
		//成功
		return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//得到赛事列表
const getMakeProjectListLibMethod = async function(data = {}) {
	var backmsg = await eventslistlib.getMakeProjectListLib(data);
	if (backmsg.data.success) {
		//成功
		return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//得到赛事
const getMakeLibMethod = async function(data = {}) {
	var backmsg = await eventslistlib.getMakeLib(data);
	if (backmsg.data.success) {
		//成功
		return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//得到赛事详情
const getMakeUserListLibMethod = async function(data = {}) {
	var backmsg = await eventslistlib.getMakeUserListLib(data);
	if (backmsg.data.success) {
		//成功
		return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}

//得到赛事排名
const getMakeRankingLibMethod = async function(data = {},filePath) {
	var backmsg = await eventslistlib.getMakeRankingLib(data,filePath);
	if (backmsg.data.success) {
		//成功
		return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}

//得到项目列表
const getSeasonAndMakeListLibMethod = async function(data = {}) {
	var backmsg = await eventslistlib.getSeasonAndMakeListLib(data);
	if (backmsg.data.success) {
			//成功
			return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}

//参加赛事
const gsetApplicationLibMethod = async function(data = {}) {
	var backmsg = await eventslistlib.setApplicationLib(data);
	if (backmsg.data.success) {
			//成功
			return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}


//得到用户名下奖品列表
const getUserPrizeapiLibMethod = async function(data = {}) {
	var backmsg = await eventslistlib.getUserPrizeapiLib(data);
	if (backmsg.data.success) {
			//成功
			return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}


//得到项目配置
const getMakeProjecParameterLibMethod = async function(data = {}) {
	var backmsg = await eventslistlib.getMakeProjecParameterLib(data);
	if (backmsg.data.success) {
			//成功
			return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}

//解析微信小程序获得的手机号
const getUserPhoneApiMethod = async function(data = {}) {
	var backmsg = await eventuserslib.getUserPhoneApi(data);
	if (backmsg.data.success) {
		if (backmsg.data.data.code == '1') {
			//成功
			return backmsg.data.data.data;
		} else {
			//失败
			Base.publicshowtoasterror(Lang.lang.reclickBind);
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//获取seesionkey
const getJscodenApiMethod = async function(data = {}) {
	var backmsg = await eventuserslib.getJscodenApi(data);
	if (backmsg.data.success) {
		if (backmsg.data.data.code == '1') {
			//成功
			return backmsg.data.data.data;
		} else {
			//失败
			Base.publicshowtoasterror(Lang.lang.reclickBind);
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}

//得到项目题库
const getMakeProjecTikuLibMethod = async function(data = {}) {
	var backmsg = await eventslistlib.getMakeProjecTikuLib(data);
	if (backmsg.data.success) {
			//成功
			return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}

//支付
const getPayParameterLibMethod = async function(data = {}) {
	var backmsg = await eventslistlib.getPayParameterLib(data);
	if (backmsg.data.success) {
			//成功
			return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//提交分数
const setProjectUserAchievementLibMethod = async function(data = {}) {
	var backmsg = await eventslistlib.setProjectUserAchievementLib(data);
	if (backmsg.data.success) {
			//成功
			return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}

//得到用户
const getUserLibMethod = async function(data = {}) {
	var backmsg = await eventuserslib.getUserLib(data);
	if (backmsg.data.success) {
			//成功
			return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//用户分享接口1
const getUserShareapiLibMethod = async function(data = {}) {
	var backmsg = await eventuserslib.getUserShareapiLib(data);
	if (backmsg.data.success) {
		//成功
		 return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//插入用户
const inserUserLibMethod = async function(data = {},filePath) {
	var backmsg = await eventuserslib.inserUserLib(data,filePath);
	console.log(backmsg);
	if (backmsg.data.success) {
		//成功
		Cache.set('user_id',backmsg.data.data.data.make_user_id);
		 uni.reLaunch({
		 	url:this.automaticurl("apply"),
		 })
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}

//修改用户
const updataUserapiLibMethod = async function(data = {},filePath) {
	var backmsg = await eventuserslib.updataUserapiLib(data,filePath);
	if (backmsg.data.success) {
		//成功
		Base.publicshowtoastsuccess('成功');
		uni.reLaunch({
			url:this.automaticurl("personalcenter"),
		})
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//用户打榜记录
const getUserProjectAchievementapiLibMethod = async function(data = {}) {
	var backmsg = await eventuserslib.getUserProjectAchievementapiLib(data);
	if (backmsg.data.success) {
		//成功
		 return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//获取用户赛事次数
const getUserAccountapiLibMethod = async function(data = {}) {
	var backmsg = await eventuserslib.getUserAccountapiLib(data);
	if (backmsg.data.success) {
		//成功
		return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//个人中心
const personalcenter = '/pages/hitgame/pages/personalcenter';
//注册
const apply = '/pages/hitgame/pages/apply';
//进入项目
const matcheslist = '/pages/hitgame/pages/matcheslist';
//赛季
const gamelist = '/pages/hitgame/pages/gamelist';
//排行榜
const ranking = '/pages/hitgame/pages/ranking';
//自动url 
const automaticurl = function(data) {
	if (data == 'apply') {return apply;}
	if (data == 'matcheslist') {return matcheslist;}
	if (data == 'personalcenter') {	return personalcenter;}
	if (data == 'gamelist') {	return gamelist;}
	if (data == 'ranking') {	return ranking;}
}

export default {
	homeUrl,
	getindexdatLibMethod,
	getSeasonListLibMethod,
	getMakeListLibMethod,
	getMakeProjectListLibMethod,
	automaticurl,
	getUserLibMethod,
	inserUserLibMethod,
	updataUserapiLibMethod,
	getMakeLibMethod,
	getMakeUserListLibMethod,
	getMakeRankingLibMethod,
	getUserShareapiLibMethod,
	gsetApplicationLibMethod,
	getSeasonAndMakeListLibMethod,
	getUserPrizeapiLibMethod,
	getUserAccountapiLibMethod,
	getwxUserLibMethod,
	getMakeProjecParameterLibMethod,
	getPayParameterLibMethod,
	getMakeProjecTikuLibMethod,
	setProjectUserAchievementLibMethod,
	getUserProjectAchievementapiLibMethod,
	getUserPhoneApiMethod,
	getJscodenApiMethod,
	getPrizeListLibMethod,
}